// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package androidx.gdata.blogger.xml;

import androidx.gdata.XMLSerializer;
import androidx.gdata.LinkUrl;
import androidx.gdata.Category;
import androidx.gdata.blogger.Blog;
import androidx.gdata.blogger.BlogContent;
import androidx.gdata.blogger.BlogEntry;
import androidx.gdata.xml.CategorySerializer;
import androidx.gdata.xml.LinkUrlSerializer;
import androidx.gdata.xml.AuthorSerializer;

import java.io.PrintWriter;

/**
 * @author jennings Date: Nov 12, 2008
 */
public class BlogEntrySerializer implements XMLSerializer<BlogEntry> {

  private static CategorySerializer categorySerializer = new CategorySerializer();
  private static LinkUrlSerializer linkSerializer = new LinkUrlSerializer();
  private static AuthorSerializer authorSerializer = new AuthorSerializer();
  private static BlogContentSerializer contentSerializer = new BlogContentSerializer();
  private static AppEditedSerializer appEditedSerializer = new AppEditedSerializer(2);
  private static AppControlSerializer appControlSerializer = new AppControlSerializer(2);
  private static ThreadTotalSerializer ttSerializer = new ThreadTotalSerializer();
  private boolean showXmlNamespace;

  static {
    categorySerializer.setExtraIndent("  ");
    linkSerializer.setExtraIndent("  ");
    authorSerializer.setExtraIndent("  ");
    contentSerializer.setExtraIndent("  ");
    ttSerializer.setExtraIndent("  ");
  }

  public BlogEntrySerializer() {
    this(false);
  }

  public BlogEntrySerializer(boolean showXmlNamespace) {
    this.showXmlNamespace = showXmlNamespace;
  }

  public void serialize(PrintWriter out, BlogEntry entry) {
    if (showXmlNamespace) {
      if (entry.getETag() == null) {
        out.println("  <entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005'>");
      } else {
        out.println("  <entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='"+ entry.getETag() +"'>");
      }
    } else {
      out.println("  <entry>");
    }
    if (entry.getId() != null) {
      out.println("    <id>" + entry.getId() + "</id>");
    }
    if (entry.getPublished() != null) {
      out.println("    <published>" + entry.getPublished() + "</published>");
    }
    if (entry.getUpdated() != null) {
      out.println("    <updated>" + entry.getUpdated() + "</updated>");
    }
    if (entry.getAppEdited() != null) {
      appEditedSerializer.serialize(out, entry.getAppEdited());
    }
    if (entry.getAppControl() != null) {
      appControlSerializer.serialize(out, entry.getAppControl());
    }
    if (entry.getCategories() != null) {
      for (Category category : entry.getCategories()) {
        categorySerializer.serialize(out, category);
      }
    }
    out.println("    <title type='text'>" + entry.getTitle() + "</title>");
    BlogContent content = entry.getContent();
    if (content != null) {
      contentSerializer.serialize(out, content);
    }
    if (entry.getSummary() != null) {
      out.println("    <summary type='html'>" + entry.getSummary() + "</summary>");
    }
    if (entry.getLinks() != null) {
      for (LinkUrl link : entry.getLinks()) {
        linkSerializer.serialize(out, link);
      }
    }
    if (entry.getAuthor() != null) {
      authorSerializer.serialize(out, entry.getAuthor());
    }
    if (entry.getThreadTotal() != null) {
      ttSerializer.serialize(out, entry.getThreadTotal());
    }
    out.println("  </entry>");
  }
}